package com.oren.system.service.imp;

import com.oren.base.pojo.Tree;
import com.oren.system.mapper.MenuMapper;
import com.oren.system.pojo.Menu;
import com.oren.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: oren
 * @Description:
 * @Date: Create on 2017/11/8 0008 14:12
 */
@Service
@Transactional
public class MenuServiceImp implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	/**
	 * 查询所有的菜单数据
	 * @return
	 * @throws Exception
	 */

	public List<Menu> listMenu() throws Exception {
		List<Menu> menuList=menuMapper.listMenu();
		List<Menu> listReturn=new ArrayList<Menu>();
		if(menuList!=null&&menuList.size()>0){
			for (Menu menu:menuList) {
				if(menu.getParentId().equals("-1")){
					listReturn.add(menu);
				}
			}
			for (Menu menu:listReturn) {
				menu.setListChild(getChild(String.valueOf(menu.getId()),menuList));
			}
		}
		return listReturn;
	}


	public List<Tree> listTreeMenu(String parentId) throws Exception {
		//根据id获取当前节点数据
		List<Menu > listk=list(parentId);
		List<Tree> treeList=listTree(listk);
		return treeList;
	}



	public List<Menu> list(String parentId)throws Exception{
		List<Menu> listMenu=new ArrayList<Menu>();
		List<Menu> menuList1=menuMapper.getMenuByParentId(Integer.parseInt(parentId));
		if(menuList1!=null&& menuList1.size()>0){
			for (int i = 0; i <menuList1.size() ; i++) {
				Menu menu=menuList1.get(i);
				menu.setListChild(list(String .valueOf(menu.getId())));
				listMenu.add(menu);
			}
		}else{
			listMenu=null;
		}
		if(listMenu==null){
			return null;
		}else{
			return  menuList1;
		}
	}

	public List<Tree> listTree(List<Menu> list){
		List<Tree> listParent=new ArrayList<Tree>();
		if(list!=null&&list.size()>0){
			for (int i = 0; i <list.size(); i++) {
				Tree tree=new Tree();
				Menu menu=list.get(i);
				tree.setId(String.valueOf(menu.getId()));
				tree.setText(menu.getMenuName());
				tree.setState("closed");
				List<Map<String,Object>> listk=new ArrayList<Map<String,Object>>();
				Map map=new HashMap();
				map.put("url",menu.getUrl());
				listk.add(map);
				tree.setAttributes(listk);
				if(menu.getListChild()!=null&&menu.getListChild().size()>0){
					tree.setChildren(listTree(menu.getListChild()));
				}
				listParent.add(tree);
			}
		}else{
			return null;
		}
		if(listParent==null){
			return null;
		}else{
			return listParent;
		}
	}


	/**
	 * 递归查找子菜单
	 * @param id 当前菜单id
	 * @param rootMenu 要查找的列表
	 * @return
	 */
	private List<Menu> getChild(String id, List<Menu> rootMenu) {
		// 子菜单
		List<Menu> childList = new ArrayList<Menu>();
		for (Menu menu : rootMenu) {
			// 遍历所有节点，将父菜单id与传过来的id比较
			if (!StringUtils.isEmpty(menu.getParentId())) {
				if (menu.getParentId().equals(id)) {
					childList.add(menu);
				}
			}
		}
		// 把子菜单的子菜单再循环一遍
		for (Menu menu : childList) {// 没有url子菜单还有子菜单
			if (!StringUtils.isEmpty(menu.getId())) {
				// 递归
				menu.setListChild(getChild(String.valueOf(menu.getId()), rootMenu));
			}
		} // 递归退出条件
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}
}
